package com.fei.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import http.HttpClientUtil;

public class GetHost {
	public static void main2(String[] args) throws Exception {
		String url = "https://www.ngrok.cc/tunnel/add";

		InputStream is = GetHost.class.getResourceAsStream("hosts-useable");
		BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));

		List<String> hostList = new ArrayList<>();
		String line = null;
		while ((line = reader.readLine()) != null) {
			hostList.add(line.toLowerCase().trim());
		}

		reader.close();
		is.close();

		for (String host : hostList) {
			Map<String, String> params = new HashMap<>();
			params.put("host", host);
			params.put("server", "1");
			params.put("protocol", "http");
			params.put("location", "127.0.0.1:80");
			params.put("name", host);

			// Cookie:UM_distinctid=15b12d45b2a3bc-0a38871d16b459-591c3116-100200-15b12d45b2b6a3;
			// PHPSESSID=21b2a0ilhgd3tvkcm26475h4r6;
			// CNZZDATA1256514333=1971343916-1487127695-https%253A%252F%252Fwww.baidu.com%252F%7C1497597899
			Map<String, String> headers = new HashMap<>();
			headers.put("Cookie",
					"UM_distinctid=15b12d45b2a3bc-0a38871d16b459-591c3116-100200-15b12d45b2b6a3; PHPSESSID=21b2a0ilhgd3tvkcm26475h4r6; CNZZDATA1256514333=1971343916-1487127695-https%253A%252F%252Fwww.baidu.com%252F%7C1497597899");

			String result = HttpClientUtil.executePost(url, headers, params);
			System.out.println(result.length());
		}

	}
	
	public static void main(String[] args) throws Exception {
		String url = "https://www.ngrok.cc/api/checked/remote.html";


		List<String> useableList = new ArrayList<>();

		for (int i = 1024 ; i < 65535;i++) {
			Map<String, String> params = new HashMap<>();
			params.put("remote", i + "");
			params.put("server", "1");

			String result = HttpClientUtil.executePost(url, params);

			if ("1".equals(result)) {
				useableList.add(i + "");
			}
			
			if((i % 1000) == 0){
				System.out.println("执行到===========" + i);
			}
		}

		OutputStream os = new FileOutputStream(new File("port-useable"));

		for (String host : useableList) {
			host = host.toLowerCase().trim() + "\n";
			os.write(host.getBytes("UTF-8"));
		}

		os.flush();
		os.close();
	}

	public static void main3(String[] args) throws Exception {
		String url = "https://www.ngrok.cc/api/checked/host.html";

		InputStream is = GetHost.class.getResourceAsStream("hosts");
		BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));

		List<String> hostList = new ArrayList<>();
		String line = null;
		while ((line = reader.readLine()) != null) {
			hostList.add(line.toLowerCase().trim());
		}

		reader.close();
		is.close();

		List<String> useableList = new ArrayList<>();

		for (String host : hostList) {
			Map<String, String> params = new HashMap<>();
			params.put("host", host);
			params.put("server", "1");

			String result = HttpClientUtil.executePost(url, params);

			if ("1".equals(result)) {
				useableList.add(host);
			}
		}

		OutputStream os = new FileOutputStream(new File("hosts-useable"));

		for (String host : useableList) {
			host = host.toLowerCase().trim() + "\n";
			os.write(host.getBytes("UTF-8"));
		}

		os.flush();
		os.close();
	}

	public static void main1(String[] args) throws Exception {
		String urlStr = "https://www.ngrok.cc/api/checked/host.html";

		Map<String, String> params = new HashMap<>();
		String hostName = "q";
		params.put("host", hostName);
		params.put("server", "1");

		URL url = new URL(urlStr);
		HttpURLConnection http = (HttpURLConnection) url.openConnection();
		http.setDoOutput(true);
		http.setDoInput(true);
		http.setRequestMethod("POST");

		http.connect();
		InputStream is = http.getInputStream();

		BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));

		String line = null;
		while ((line = br.readLine()) != null) {
			System.out.println(line);
		}
	}
}
